package com.inspectortime.webapp.inspector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.repository.Factory;
import com.inspectortime.repository.Repository;
import com.inspectortime.util.SpringValidatorUtils;
import com.inspectortime.webapp.util.HttpSessionUtils;

/**
 * Edit Authorize.net payment gateway info
 *
 */
public class EditPaymentGatewayController extends SimpleFormController {
	
	private static Log log = LogFactory
	.getLog(EditPaymentGatewayController.class);
	private Repository repository;
	private Factory factory;
	
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		InspectorCompany company = HttpSessionUtils.retrieveLoggedInCompany(
				request, repository);
		return company;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,	HttpServletResponse response, Object command, BindException errors)
		throws Exception {
		
		InspectorCompany company = (InspectorCompany) command;
		company.save();
		
		return new ModelAndView(getSuccessView(), errors.getModel());
	}
	
	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception {
		SpringValidatorUtils.logValidationErrors(log, errors);
	}
	
	public void setRepository(Repository repository) {
		this.repository = repository;
	}
	
	public void setFactory(Factory factory) {
		this.factory = factory;
	}

}
